home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / cp_hash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  6.3 KB  |  240 lines

  1. /*  cp-hash.c  -- file copying (hash search routines)
  2.     Copyright (C) 1989, 1990 Free Software Foundation.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.     Written by Torbjorn Granlund, Sweden (tege@sics.se). */
  19.  
  20. /*
  21.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  22.  *
  23.  * To this port, the same copying conditions apply as to the
  24.  * original release.
  25.  *
  26.  * IMPORTANT:
  27.  * This file is not identical to the original GNU release!
  28.  * You should have received this code as patch to the official
  29.  * GNU release.
  30.  *
  31.  * MORE IMPORTANT:
  32.  * This port comes with ABSOLUTELY NO WARRANTY.
  33.  *
  34.  * $Header: e:/gnu/fileutil/RCS/cp-hash.c'v 1.3.0.2 90/06/29 00:46:35 tho Stable $
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include "cp.h"
  39.  
  40. #ifdef MSDOS
  41. char *hash_insert(unsigned short,short,char *);
  42. char *hash_insert2(struct htab *,unsigned short,short,char *);
  43. #endif /* MSDOS */
  44.  
  45. char *hash_insert ();
  46. char *hash_insert2 ();
  47.  
  48. struct htab *htab;
  49. char new_file;
  50.  
  51. /* Add PATH to the list of files that we have created.
  52.    Return 0 if successful, 1 if not. */
  53.  
  54. int
  55. remember_created (path)
  56.      char *path;
  57. {
  58.   struct stat sb;
  59.  
  60.   if (stat (path, &sb) < 0)
  61.     {
  62.       error (0, errno, "%s", path);
  63.       return 1;
  64.     }
  65.  
  66.   hash_insert (sb.st_ino, sb.st_dev, &new_file);
  67.   return 0;
  68. }
  69.  
  70. /* Add path NODE, copied from inode number INO and device number DEV,
  71.    to the list of files we have copied.
  72.    Return NULL if inserted, otherwise non-NULL. */
  73.  
  74. char *
  75. remember_copied (node, ino, dev)
  76.      char *node;
  77.      ino_t ino;
  78.      dev_t dev;
  79. {
  80.   return hash_insert (ino, dev, node);
  81. }
  82.  
  83. /* Allocate space for the hash structures, and set the global
  84.    variable `htab' to point to it.  The initial hash module is specified in
  85.    MODULUS, and the number of entries are specified in ENTRY_TAB_SIZE.  (The
  86.    hash structure will be rebuilt when ENTRY_TAB_SIZE entries have been
  87.    inserted, and MODULUS and ENTRY_TAB_SIZE in the global `htab' will be
  88.    doubled.)  */
  89.  
  90. void
  91. hash_init (modulus, entry_tab_size)
  92.      unsigned modulus;
  93.      unsigned entry_tab_size;
  94. {
  95.   struct htab *htab_r;
  96.  
  97.   htab_r = (struct htab *)
  98.     xmalloc (sizeof (struct htab) + sizeof (struct entry *) * modulus);
  99.  
  100.   htab_r->entry_tab = (struct entry *)
  101.     xmalloc (sizeof (struct entry) * entry_tab_size);
  102.  
  103.   htab_r->modulus = modulus;
  104.   htab_r->entry_tab_size = entry_tab_size;
  105.   htab = htab_r;
  106.  
  107.   forget_all ();
  108. }
  109.  
  110. /* Reset the hash structure in the global variable `htab' to
  111.    contain no entries.  */
  112.  
  113. void
  114. forget_all ()
  115. {
  116.   int i;
  117.   struct entry **p;
  118.  
  119.   htab->first_free_entry = 0;
  120.  
  121.   p = htab->hash;
  122.   for (i = htab->modulus; i > 0; i--)
  123.     *p++ = NULL;
  124. }
  125.  
  126. /* Insert path NODE, copied from inode number INO and device number DEV,
  127.    into the hash structure in the global variable `htab', if an entry with
  128.    the same inode and device was not found already.
  129.    Return NULL if inserted, otherwise non-NULL. */
  130.  
  131. char *
  132. hash_insert (ino, dev, node)
  133.      ino_t ino;
  134.      dev_t dev;
  135.      char *node;
  136. {
  137.   struct htab *htab_r = htab;
  138.  
  139.   if (htab_r->first_free_entry >= htab_r->entry_tab_size)
  140.     {
  141.       int i;
  142.       struct entry *ep;
  143.       unsigned modulus;
  144.       unsigned entry_tab_size;
  145.  
  146.       /* Increase the number of hash entries, and re-hash the data.
  147.      The method of shrinking and increasing is made to compactify
  148.      the heap.  If twice as much data would be allocated
  149.      straightforwardly, we would never re-use a byte of memory.  */
  150.  
  151.       /* Let htab shrink.  Keep only the header, not the pointer vector.  */
  152.  
  153.       htab_r = (struct htab *)
  154.     xrealloc ((char *) htab_r, sizeof (struct htab));
  155.  
  156.       modulus = 2 * htab_r->modulus;
  157.       entry_tab_size = 2 * htab_r->entry_tab_size;
  158.  
  159.       /* Increase the number of possible entries.  */
  160.  
  161.       htab_r->entry_tab = (struct entry *)
  162.     xrealloc ((char *) htab_r->entry_tab,
  163.           sizeof (struct entry) * entry_tab_size);
  164.  
  165.       /* Increase the size of htab again.  */
  166.  
  167.       htab_r = (struct htab *)
  168.     xrealloc ((char *) htab_r,
  169.           sizeof (struct htab) + sizeof (struct entry *) * modulus);
  170.  
  171.       htab_r->modulus = modulus;
  172.       htab_r->entry_tab_size = entry_tab_size;
  173.       htab = htab_r;
  174.  
  175.       i = htab_r->first_free_entry;
  176.  
  177.       /* Make the increased hash table empty.  The entries are still
  178.      available in htab->entry_tab.  */
  179.  
  180.       forget_all ();
  181.  
  182.       /* Go through the entries and install them in the pointer vector
  183.      htab->hash.  The items are actually inserted in htab->entry_tab at
  184.      the position where they already are.  The htab->coll_link need
  185.      however be updated.  Could be made a little more efficient.  */
  186.  
  187.       for (ep = htab_r->entry_tab; i > 0; i--)
  188.     {
  189.       hash_insert2 (htab_r, ep->ino, ep->dev, ep->node);
  190.       ep++;
  191.     }
  192.     }
  193.  
  194.   return hash_insert2 (htab_r, ino, dev, node);
  195. }
  196.  
  197. /* Insert path NODE, copied from inode number INO and device number DEV,
  198.    into the hash structure HTAB, if not already present.
  199.    Return NULL if inserted, otherwise non-NULL. */
  200.  
  201. char *
  202. hash_insert2 (htab, ino, dev, node)
  203.      struct htab *htab;
  204.      ino_t ino;
  205.      dev_t dev;
  206.      char *node;
  207. {
  208.   struct entry **hp, *ep2, *ep;
  209.   hp = &htab->hash[ino % htab->modulus];
  210.   ep2 = *hp;
  211.  
  212.   /* Collision?  */
  213.  
  214.   if (ep2 != NULL)
  215.     {
  216.       ep = ep2;
  217.  
  218.       /* Search for an entry with the same data.  */
  219.  
  220.       do
  221.     {
  222.       if (ep->ino == ino && ep->dev == dev)
  223.         return ep->node;    /* Found an entry with the same data.  */
  224.       ep = ep->coll_link;
  225.     }
  226.       while (ep != NULL);
  227.  
  228.       /* Did not find it.  */
  229.  
  230.     }
  231.  
  232.   ep = *hp = &htab->entry_tab[htab->first_free_entry++];
  233.   ep->ino = ino;
  234.   ep->dev = dev;
  235.   ep->node = node;
  236.   ep->coll_link = ep2;        /* ep2 is NULL if not collision.  */
  237.  
  238.   return NULL;
  239. }
  240.